package com.dx.mes.service.process.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.table.rtnBean.RouteObj;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.process.service.IFppunpackclmService;
import com.dx.mes.facade.process.tx.Fppupkclm.FppupkclmI;
import com.dx.mes.facade.process.tx.Fppupkclm.FppupkclmIA;
import com.dx.mes.facade.process.tx.Fppupkclm.FppupkclmO;
import com.dx.mes.facade.process.tx.Fppupkclm.FppupkclmOA;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fppunpackclmService")
public class FppunpackclmService implements IFppunpackclmService{
	private LogUtils logUtils;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IBisMdlGrdDao bisMdlGrdDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IRetCrtNoDao retCrtNoDao;
	@Autowired
	private IRetToolStDao retToolStDao;
	@Autowired
	private IBisBomDao bisBomDao;
	@Autowired
	private IBisToolGrDao bisToolGrDao;
	@Autowired
	private IRetPrdIndtDao retPrdIndtDao;
	@Autowired
	private IRetWoBomDao   retWoBomDao;

	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FppunpackclmService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FppunpackclmService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");

		String strOutMsg = null;
		FppupkclmI inTrx = new FppupkclmI();
		FppupkclmO outTrx = new FppupkclmO();

		Session session = retPrdInfoDao.getCrSession();

		outTrx.setTrx_id(T_FPPUPKCLM);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FppupkclmI.class);

			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
				case ACTION_FLG_QUERY:
					queryByConditionFnc(inTrx,outTrx);
					break;
				case ACTION_FLG_ADD:
					addFnc(inTrx,outTrx);
					break;
				case ACTION_FLG_DELETE:
					delFnc(inTrx,outTrx);
					break;
				case 'W':
					wRlsFnc(inTrx,outTrx);
					break;
                case ACTION_PRD_FLG_QUERY:
                    queryByConditionFnc1(inTrx,outTrx);
                    break;
				default:
					outTrx = errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg("程序执行异常");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutMsg + "]");
		}
		return strOutMsg;
	}
	public void queryByConditionFnc(FppupkclmI inTrx,FppupkclmO outTrx) {
		FppupkclmIA iary=inTrx.getIary().get(0);
		String woId = iary.getWo_id();
		String prdSeqId = iary.getPrd_seq_id();
		String lotId = iary.getLot_id();
		String prdStat = iary.getPrd_stat();
		String woCate = iary.getWo_cate();
		String woIdFk = "";
		if(!Util.isSpaceCheck(prdSeqId)){
			Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
			woIdFk = ret_prd_info.getWo_id_fk();
		}else {
			woIdFk = woId;
		}
		Ret_wo ret_wo = retWoDao.get(woIdFk);
		if(ret_wo==null){
			outTrx.setRtn_code(E_RET_WO + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("工单号[" + woId + "]信息不存在");
			return;
		}
			Bis_pathiId bis_pathiId = new Bis_pathiId();
			bis_pathiId.setPath_id(ret_wo.getPath_id_fk());
			bis_pathiId.setPath_ver(ret_wo.getPath_ver_fk());
			Bis_pathi bis_pathi = bisPathiDao.get(bis_pathiId);
			if(bis_pathi==null &&  bis_pathi.getFirst_ope_no().equals("1") ){
				outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND + "");
				outTrx.setRtn_mesg("工艺路线[" + ret_wo.getPath_id_fk()
						+ "][" + ret_wo.getPath_ver_fk() + "]不存在");
				return;
			}
        String hql = "FROM Ret_prd_info where 1=1 and nx_ope_no_fk ='" + bis_pathi.getFirst_ope_no() + "'";
		if (prdSeqId != null && prdSeqId.length() != 0) {
			hql = hql + " and prd_seq_id ='" + prdSeqId + "'";
		}
		if (prdStat != null && prdStat.length() != 0) {
			hql = hql + " and prd_stat ='" + prdStat + "'";
		}
		if (lotId != null && lotId.length() != 0) {
			hql = hql + " and lot_id ='" + lotId + "'";
		}
		if (woId != null && woId.length() != 0) {
			hql = hql + " and wo_id_fk ='" + woId + "'";
		}
		//and nx_toolg_id_fk = '" + bis_ope.getToolg_id_fk() + "'
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql);
        if(ret_prd_infos.get(0).getCr_ope_no_fk().equals(bis_pathi.getFirst_ope_no())){
            outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND + "");
            outTrx.setRtn_mesg("站点[" + bis_pathi.getFirst_ope_no()
                    + "][" + ret_prd_infos.get(0).getCr_ope_no_fk() + "]不一致");
            return;
        }
		List<FppupkclmOA> oary = new ArrayList<FppupkclmOA>();
		for(Ret_prd_info ret_prd_info : ret_prd_infos ){
			FppupkclmOA   fppupkclmOA = new FppupkclmOA();
			String vdr_prd_seq_id = ret_prd_info.getPrd_seq_id();
			Ret_prd_indt ret_prd_indt = retPrdIndtDao.get(vdr_prd_seq_id);
			if(null != ret_prd_indt){
				fppupkclmOA.setMtrl_box_id(ret_prd_indt.getMtrl_box_id_fk());
				Ret_prd_in ret_prd_in = retPrdInDao.get(ret_prd_indt.getMtrl_box_id_fk());
				if(null != ret_prd_in && null != ret_prd_in.getPrd_act_qty()){
					String prd_act_qty = String.valueOf(ret_prd_in.getPrd_act_qty());
					fppupkclmOA.setPrd_act_qty(prd_act_qty);
				}
			}
			fppupkclmOA.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
			fppupkclmOA.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
			fppupkclmOA.setWo_id_fk(ret_prd_info.getWo_id_fk());
			fppupkclmOA.setPrd_stat(ret_prd_info.getPrd_stat());
			fppupkclmOA.setPrd_grade(ret_prd_info.getPrd_grade());
			fppupkclmOA.setSo_id_fk(ret_prd_info.getSo_id_fk());
//			fppupkclmOA.setCr_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
			fppupkclmOA.setEvt_usr(ret_prd_info.getEvt_usr());
			fppupkclmOA.setEvt_timestamp(ret_prd_info.getEvt_timestamp());
			oary.add(fppupkclmOA);
		}
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;

	}
    public void queryByConditionFnc1(FppupkclmI inTrx,FppupkclmO outTrx) {
        FppupkclmIA iary = inTrx.getIary().get(0);
        String woId = iary.getWo_id();
        String prdSeqId = iary.getPrd_seq_id();
        String prdStat = iary.getPrd_stat();
        Ret_wo ret_wo = retWoDao.get(woId);
        if (ret_wo == null) {
            outTrx.setRtn_code(E_RET_WO + E_READ_NOT_FOUND + "");
            outTrx.setRtn_mesg("工单号[" + woId + "]信息不存在");
            return;
        }
       String hqlM="FROM Bis_path where path_id_fk ='" + ret_wo.getPath_id_fk() + "' " +
               "and path_ver_fk='" + ret_wo.getPath_ver_fk() + "'  and  pv_ope_no = 'FRST' ";
        List<Bis_path>  bis_pathList = bisPathDao.find(hqlM);
        List<FppupkclmOA> oary = new ArrayList<FppupkclmOA>();
        if(bis_pathList!=null  && bis_pathList.size()>0){
            String hql = "FROM Ret_prd_info where nx_ope_id_fk ='" + bis_pathList.get(0).getCr_ope_id_fk() + "'";
            if (prdSeqId != null && prdSeqId.length() != 0) {
                hql = hql + " and prd_seq_id ='" + prdSeqId + "'";
            }
            if (prdStat != null && prdStat.length() != 0) {
                hql = hql + " and prd_stat ='" + prdStat + "'";
            }
            if (woId != null && woId.length() != 0) {
                hql = hql + " and wo_id_fk ='" + woId + "'";
            }
            List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql);
            for (Ret_prd_info ret_prd_info : ret_prd_infos) {
                FppupkclmOA oary1 = new FppupkclmOA();
                oary1.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
                oary1.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
                oary1.setWo_id_fk(ret_prd_info.getWo_id_fk());
                oary1.setPrd_stat(ret_prd_info.getPrd_stat());
                oary1.setPrd_grade(ret_prd_info.getPrd_grade());
                oary1.setSo_id_fk(ret_prd_info.getSo_id_fk());
                oary1.setEvt_usr(ret_prd_info.getEvt_usr());
                oary1.setEvt_timestamp(ret_prd_info.getEvt_timestamp());
                oary.add(oary1);
            }
        }
        outTrx.setOary(oary);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }
	public void addFnc(FppupkclmI inTrx,FppupkclmO outTrx) {

		Timestamp current_timestamp = Util.getCurrentTimestamp();
		FppupkclmIA iary=inTrx.getIary().get(0);
		String evtUsr = inTrx.getEvt_usr();
		String woId = iary.getWo_id();
		String prdSeqId = iary.getPrd_seq_id();
		String lotId = iary.getLot_id();
		String prdGrade = iary.getPrd_grade();
		String rsnCode  = iary.getRsn_code();
		String mtrlIdFk = iary.getMtrl_id_fk();
		String mtrlBoxId = iary.getMtrl_box_id();


		//检查玻璃信息是否存在
		Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
		if(ret_prd_info != null){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("屏体代码[" + prdSeqId + "]已经存在");
			return;
		}
		//获取来料玻璃信息
		/*Ret_prd_indt ret_prd_indt = retPrdIndtDao.get(prdSeqId);
		if(null == ret_prd_indt){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("屏体代码[" + prdSeqId + "]在来料信息表中不存在");
			return;
		}
		//解封的工单与工单bom的是否一致
		Ret_prd_in  ret_prd_in = retPrdInDao.get(ret_prd_indt.getMtrl_box_id_fk());
		String hqlM="From Ret_wo_bom where wo_id_fk='"+woId+"' and mtrl_prod_id_fk='"+ret_prd_in.getMtrl_prod_id_fk()+"'";
		List<Ret_wo_bom> ret_wo_boms = retWoBomDao.find(hqlM);
		if(ret_wo_boms==null || ret_wo_boms.size()<=0){
			outTrx.setRtn_code(E_FDPAPLYWO_RLSE_ERROR+ "");
			outTrx.setRtn_mesg("工单["+woId+"]与来料屏体的工单BOM不一致");
			return;
		}*/
//		if(!mtrlBoxId.equals(ret_prd_indt.getMtrl_box_id_fk())){
//			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
//			outTrx.setRtn_mesg("所属来料箱号[" + ret_prd_indt.getMtrl_box_id_fk() + "]与刷入来料箱号[" + mtrlBoxId + "]不符");
//			return;
//		}
//		if(ret_prd_indt.getPrd_stat().equals("RLSE")){
//			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
//			outTrx.setRtn_mesg("玻璃[" + prdSeqId + "]已经下线");
//			return;
//		}
		//获取来料箱信息
//		Ret_prd_in ret_prd_in = retPrdInDao.get(mtrlBoxId);
//		if(null==mtrlBoxId){
//			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
//			outTrx.setRtn_mesg("来料箱[" + mtrlBoxId + "]不存在");
//			return;
//		}
//		if(ret_prd_in.getPrd_act_qty()==0){
//			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
//			outTrx.setRtn_mesg("来料箱[" + mtrlBoxId + "]可释放玻璃数为0");
//			return;
//		}
		//获取工单信息
		Ret_wo ret_wo = retWoDao.get(woId);
		if(ret_wo==null){
			outTrx.setRtn_code(E_RET_WO + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("未找到工单代码[" + woId + "]的信息");
			return;
		}else{
			if(ret_wo.getRl_prd_qty() + 1 > ret_wo.getPln_prd_qty()){
				outTrx.setRtn_code(E_FPPUPKCLM_QTY_ERROR + "");
				outTrx.setRtn_mesg("工单代码[" + ret_wo.getWo_id() + "]计划数量为[" + ret_wo.getPln_prd_qty() 
						+ "],实际下线数量为[" + ret_wo.getRl_prd_qty()
						+ "],无法解封,请确认 ");
				return;
			}
			if(!ret_wo.getWo_stat().equals(WO_STATUS_WAIT) && !ret_wo.getWo_stat().equals(WO_STATUS_RLSE)){
				outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
				outTrx.setRtn_mesg("工单代码[" + ret_wo.getWo_id() 
						+ "]状态[" + ret_wo.getWo_stat() 
						+ "]不正确，无法解封,请确认 !");
				return;
			}
		}
		
		//获取产品信息
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
		if(bis_mdl_def==null){
			outTrx.setRtn_code(E_BIS_MDL_DEF + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("未找到产品代码[" + ret_wo.getMdl_id_fk() + "]的信息");
			return;
		}
		
        //获取工艺路线信息
		Bis_pathiId bis_pathiId = new Bis_pathiId();
		bis_pathiId.setPath_id(ret_wo.getPath_id_fk());
		bis_pathiId.setPath_ver(ret_wo.getPath_ver_fk());
		Bis_pathi bis_pathi = bisPathiDao.get(bis_pathiId);
		if(bis_pathi==null){
			outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("未找到工艺路线[" + ret_wo.getPath_id_fk() 
					+ "][" + ret_wo.getPath_ver_fk() + "]的信息");
			return;
		}
		//check pathi hold
		String path_stat = bis_pathi.getPath_stat();
		if(PROC_HOLD.equals(path_stat)){
			outTrx.setRtn_code(E_FPPUPKCLM_PATH_STATUS_INVALIED + "");
			outTrx.setRtn_mesg("该订单的工艺路线已经HOLD");
			return;
		}
	    //检查工艺路线起始站点设定
		Bis_pathId bis_pathId = new Bis_pathId();
		bis_pathId.setPath_id_fk(bis_pathi.getId().getPath_id());
		bis_pathId.setPath_ver_fk(bis_pathi.getId().getPath_ver());
		bis_pathId.setCr_ope_no(bis_pathi.getFirst_ope_no());
		Bis_path bis_path = bisPathDao.get(bis_pathId);
		if(bis_path==null){
			outTrx.setRtn_code(E_FPPUPKCLM_PATH_NO_FRST_OPE + "");
			outTrx.setRtn_mesg("工艺路线[" + bis_pathi.getId().getPath_id() 
					+ "][" + bis_pathi.getId().getPath_ver() + "]未设置起始站点");
			return;
		}
		
		//获取起始站点信息
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
		bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());
		Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
		if(bis_ope==null){
			outTrx.setRtn_code(E_BIS_OPE + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("站点[" + bis_path.getCr_ope_id_fk() 
					+ "][" + bis_path.getCr_ope_ver_fk() + "]信息没有维护");
			return;
		}
		
		//获取下一制程站点信息
//		Bis_path nx_bis_path = new Bis_path();
//		Bis_ope nx_bis_ope = new Bis_ope();
//		RouteObj nxRouteObj = bisPathDao.getNxPath(bis_path.getId().getPath_id_fk(),
//				bis_path.getId().getPath_ver_fk(), bis_path.getId().getCr_ope_no());
//		if(_NORMAL == nxRouteObj.getLRc()){
//			nx_bis_path = nxRouteObj.getPath();
//			nx_bis_ope = nxRouteObj.getOpe();
//		}else if(PATH_IS_FINISH == nxRouteObj.getLRc()){
//			;
//		}else{
//			outTrx.setRtn_code(E_BIS_OPE + E_READ_NOT_FOUND + "");
//			outTrx.setRtn_mesg("获取下一制程站点错误：" + nxRouteObj.getGszMsgBuf());
//			return;
//		}

		//Set Prd_info
		ret_prd_info = new Ret_prd_info();
		ret_prd_info.setPrd_seq_id(prdSeqId);
		if(_NG.equals(prdGrade)){
		    ret_prd_info.setExt_9("NG");
        }else {
		    ret_prd_info.setExt_9("OK");
        }
		ret_prd_info.setWo_id_fk(woId);
		ret_prd_info.setLot_id(lotId);
		ret_prd_info.setRsn_code(rsnCode);
		ret_prd_info.setMdl_id_fk(bis_mdl_def.getMdl_id());
		ret_prd_info.setSo_id_fk(ret_wo.getSo_id());
		ret_prd_info.setMtrl_prod_id_fk(mtrlIdFk);
		ret_prd_info.setPrd_seq_typ(PRD_TYPE_PANEL);
		ret_prd_info.setPrd_qty(1);
        ret_prd_info.setEvt_timestamp(current_timestamp);
		/*ret_prd_info.setEvt_usr(evtUsr);
		ret_prd_info.setEvt_cate(EVT_PNL_UNPACK);
		ret_prd_info.setEvt_timestamp(current_timestamp);
		ret_prd_info.setBatch_no(woId);
		ret_prd_info.setMtrl_box_id_fk(mtrlBoxId);
		retPrdInfoDao.save(ret_prd_info);*/
//		if(_OK.equals(prdGrade)){
////			String hql = "From Bis_mdl_grd where 1=1 and cus_id_fk ='Y'and prd_judge ='OK'";
////			List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql);
////			if(bis_mdl_grds.size() >0){
////				prdGrade =bis_mdl_grds.get(0).getPrd_grade();
////			}else {
////				prdGrade = "";
////			}
//			ret_prd_info.setPrd_grade(prdGrade);
//			ret_prd_info.setPrd_stat(PROC_IN_PROCESS);
//	        ret_prd_info.setCr_toolg_id_fk(bis_ope.getToolg_id_fk());
//			ret_prd_info.setCr_ope_id_fk(bis_path.getCr_ope_id_fk());
//			ret_prd_info.setCr_ope_no_fk(bis_path.getId().getCr_ope_no());
//			ret_prd_info.setCr_ope_ver_fk(bis_path.getCr_ope_ver_fk());
//			ret_prd_info.setCr_path_id_fk(bis_path.getId().getPath_id_fk());
//			ret_prd_info.setCr_path_ver_fk(bis_path.getId().getPath_ver_fk());
//			ret_prd_info.setCr_proc_id_fk(bis_ope.getProc_id());
//			ret_prd_info.setBnk_flg(BANK_OUT);
//			//根据设备群组获取设备代码
//			List<Bis_tool_gr> bis_tool_grList = bisToolGrDao.find("FROM Bis_tool_gr WHERE toolg_id='" + bis_ope.getToolg_id_fk() + "'");
//			if(bis_tool_grList.isEmpty()){
//				outTrx.setRtn_code(E_BIS_TOOL_GR + E_READ_NOT_FOUND + "");
//				outTrx.setRtn_mesg("未找到设备群组[" + bis_ope.getToolg_id_fk() + "]设定的设备信息");
//				return;
//			}
//			ret_prd_info.setCr_tool_id_fk(bis_tool_grList.get(0).getId().getTool_id_fk());
//
//			if(PATH_IS_FINISH == nxRouteObj.getLRc()){
//				ret_prd_info.setNx_toolg_id_fk(_SPACE);
//				ret_prd_info.setNx_ope_id_fk(_SPACE);
//				ret_prd_info.setNx_ope_no_fk(_SPACE);
//				ret_prd_info.setNx_ope_ver_fk(_SPACE);
//				ret_prd_info.setNx_path_id_fk(_SPACE);
//				ret_prd_info.setNx_path_ver_fk(_SPACE);
//				ret_prd_info.setNx_proc_id_fk(_SPACE);
//				ret_prd_info.setNx_tool_run_mode(_SPACE);
//			}else{
//				ret_prd_info.setNx_toolg_id_fk(nx_bis_ope.getToolg_id_fk());
//				ret_prd_info.setNx_ope_id_fk(nx_bis_path.getCr_ope_id_fk());
//				ret_prd_info.setNx_ope_no_fk(nx_bis_path.getId().getCr_ope_no());
//				ret_prd_info.setNx_ope_ver_fk(nx_bis_path.getCr_ope_ver_fk());
//				ret_prd_info.setNx_path_id_fk(nx_bis_path.getId().getPath_id_fk());
//				ret_prd_info.setNx_path_ver_fk(nx_bis_path.getId().getPath_ver_fk());
//				ret_prd_info.setNx_proc_id_fk(nx_bis_ope.getProc_id());
//				ret_prd_info.setNx_tool_run_mode(nx_bis_ope.getTool_run_mode());
//			}
//		}else{
//			String hql = "From Bis_mdl_grd where 1=1 and cus_id_fk ='Y'and prd_judge ='NG'";
//			List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql);
//			if(bis_mdl_grds.size() >0){
//				prdGrade =bis_mdl_grds.get(0).getPrd_grade();
//			}else {
//				prdGrade = "";
//			}
			ret_prd_info.setPrd_grade(prdGrade);
			ret_prd_info.setPrd_stat(PROC_WAIT);
			ret_prd_info.setNx_toolg_id_fk(bis_ope.getToolg_id_fk());
			ret_prd_info.setNx_ope_id_fk(bis_path.getCr_ope_id_fk());
			ret_prd_info.setNx_ope_no_fk(bis_path.getId().getCr_ope_no());
			ret_prd_info.setNx_ope_ver_fk(bis_path.getCr_ope_ver_fk());
			ret_prd_info.setNx_path_id_fk(bis_path.getId().getPath_id_fk());
			ret_prd_info.setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
			ret_prd_info.setNx_proc_id_fk(bis_ope.getProc_id());
//		}
	
		ret_prd_info.setEvt_usr(evtUsr);
		ret_prd_info.setEvt_cate(EVT_PNL_UNPACK);
		ret_prd_info.setEvt_timestamp(current_timestamp);
		ret_prd_info.setBatch_no(woId);
		ret_prd_info.setMtrl_box_id_fk(mtrlBoxId);
		retPrdInfoDao.save(ret_prd_info);
		
		//更新工单信息
		if(ret_wo.getRl_prd_qty()==0){
			ret_wo.setAct_stb_timestamp(current_timestamp);//第一片投产时间
		}
		ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty() + 1);
		ret_wo.setWo_stat(WO_STATUS_RLSE);
		ret_wo.setEvt_usr(evtUsr);
		ret_wo.setEvt_timestamp(current_timestamp);


		retWoDao.save(ret_wo);
//		ret_prd_indt.setEvt_usr(evtUsr);
//		ret_prd_indt.setEvt_timestamp(current_timestamp);
//		ret_prd_indt.setPrd_stat("RLSE");
//		retPrdIndtDao.update(ret_prd_indt);
//		ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty()-1);
//        ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty()+1);
//		ret_prd_in.setEvt_usr(evtUsr);
//		ret_prd_in.setEvt_timestamp(current_timestamp);
//        retPrdInDao.update(ret_prd_in);
		
		List<FppupkclmOA> oary = new ArrayList<FppupkclmOA>();
		FppupkclmOA fppupkclmOA = new FppupkclmOA();
		fppupkclmOA.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
		fppupkclmOA.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
		fppupkclmOA.setWo_id_fk(ret_prd_info.getWo_id_fk());
		fppupkclmOA.setPrd_stat(ret_prd_info.getPrd_stat());
		fppupkclmOA.setPrd_grade(ret_prd_info.getPrd_grade());
		fppupkclmOA.setSo_id_fk(ret_prd_info.getSo_id_fk());
		fppupkclmOA.setEvt_usr(ret_prd_info.getEvt_usr());
		fppupkclmOA.setEvt_timestamp(ret_prd_info.getEvt_timestamp());
		oary.add(fppupkclmOA);
		outTrx.setRl_prd_qty(ret_wo.getRl_prd_qty());
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}

	public void delFnc(FppupkclmI inTrx,FppupkclmO outTrx) {
		FppupkclmIA iary=inTrx.getIary().get(0);
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String evtUsr = inTrx.getEvt_usr();
		String prdSeqId = iary.getPrd_seq_id();
        //检查玻璃信息是否存在
		Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
		if(ret_prd_info == null){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]不存在");
			return;
		}
		if("INPR".equals(ret_prd_info.getPrd_stat())){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]在制程中，无法取消下线，请先取消入账");
			return;
		}
		Ret_wo ret_wo = retWoDao.get(ret_prd_info.getWo_id_fk());
		if(ret_wo == null){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]的工单信息不存在");
			return;
		}
		ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty() - 1);
	/*	ret_wo.setWo_stat(WO_STATUS_WAIT);*/
		ret_wo.setEvt_usr(evtUsr);
		ret_wo.setEvt_timestamp(current_timestamp);
		retWoDao.update(ret_wo);
		ret_prd_info.setEvt_usr(evtUsr);
		ret_prd_info.setEvt_cate(EVT_PNL_UNPACK_CANCEL);
		ret_prd_info.setEvt_timestamp(current_timestamp);
		retPrdInfoDao.delete(ret_prd_info);
//		Ret_prd_indt ret_prd_indt = retPrdIndtDao.get(prdSeqId);
//		if(null == ret_prd_indt){
//			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
//			outTrx.setRtn_mesg("玻璃代码[" + prdSeqId + "]在来料信息表中不存在");
//			return;
//		}
//		ret_prd_indt.setEvt_timestamp(current_timestamp);
//		ret_prd_indt.setEvt_usr(evtUsr);
//		ret_prd_indt.setPrd_stat("WFRL");
//		retPrdIndtDao.update(ret_prd_indt);
//		Ret_prd_in ret_prd_in = retPrdInDao.get(ret_prd_info.getMtrl_box_id_fk());
//		if(null==ret_prd_in){
//			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
//			outTrx.setRtn_mesg("来料箱[" + ret_prd_info.getMtrl_box_id_fk() + "]不存在");
//			return;
//		}
//		ret_prd_in.setEvt_timestamp(current_timestamp);
//		ret_prd_in.setEvt_usr(evtUsr);
//		ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty()+1);
//		ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty()-1);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	public FppupkclmO errActionFnc() {
		FppupkclmO FppupkclmO = new FppupkclmO();
		FppupkclmO.setTrx_id(T_FPPUPKCLM);
		FppupkclmO.setType_id(TRX_OUT);
		FppupkclmO.setRtn_code(E_FPPUPKCLM_INVALID_INPUT + "");
		FppupkclmO.setRtn_mesg("Invalid Action_flg");
		return FppupkclmO;
	}

	public void wRlsFnc(FppupkclmI inTrx,FppupkclmO outTrx) {

		Timestamp current_timestamp = Util.getCurrentTimestamp();
		FppupkclmIA iary=inTrx.getIary().get(0);
		String evtUsr = inTrx.getEvt_usr();
		String woId = iary.getWo_id();
		String lotId = iary.getLot_id();
		String prdGrade = iary.getPrd_grade();
		String rsnCode  = iary.getRsn_code();
		String mtrlIdFk = iary.getMtrl_id_fk();
		String mtrlBoxId = iary.getMtrl_box_id();

		//获取来料箱信息
		Ret_prd_in ret_prd_in = retPrdInDao.get(mtrlBoxId);
		if(null==mtrlBoxId){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("来料箱[" + mtrlBoxId + "]不存在");
			return;
		}
		if(ret_prd_in.getPrd_act_qty()==0){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("来料箱[" + mtrlBoxId + "]可释放玻璃数为0");
			return;
		}
		String hql = "FROM Ret_prd_indt where mtrl_box_id_fk='" + mtrlBoxId + "' and prd_stat='WFRL'";
		List<Ret_prd_indt> prds = retPrdIndtDao.find(hql);
		if(prds.size()==0){
			outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
			outTrx.setRtn_mesg("来料信息表中未找到可释放的玻璃");
			return;
		}else{
			if(prds.size()!=ret_prd_in.getPrd_act_qty()){
				outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
				outTrx.setRtn_mesg("来料信息表中可释放的玻璃数量为["+prds.size()+"]与来料箱中剩余的玻璃数["+ret_prd_in.getPrd_act_qty()+"]不符");
				return;
			}
		}
		//获取工单信息
		Ret_wo ret_wo = retWoDao.get(woId);
		if(ret_wo==null){
			outTrx.setRtn_code(E_RET_WO + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("工单号[" + woId + "]信息不存在");
			return;
		}else{
			if(ret_wo.getRl_prd_qty() + prds.size() > ret_wo.getPln_prd_qty()){
				outTrx.setRtn_code(E_FPPUPKCLM_QTY_ERROR + "");
				outTrx.setRtn_mesg("工单号["+ret_wo.getWo_id()+"]计划数量为["+ret_wo.getPln_prd_qty()+
						"],实际下线数量为["+ret_wo.getRl_prd_qty()+"],无法再解封玻璃,请确认 ");
				return;
			}
			if(!ret_wo.getWo_stat().equals(WO_STATUS_WAIT) && !ret_wo.getWo_stat().equals(WO_STATUS_RLSE)){
				outTrx.setRtn_code(E_FPPUPKCLM_STATE_ERROR + "");
				outTrx.setRtn_mesg("工单号["+ret_wo.getWo_id()+"]状态["+ret_wo.getWo_stat()+
						"]不正确，无法再解封,请确认 !");
				return;
			}
		}

		//获取产品信息
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
		if(bis_mdl_def==null){
			outTrx.setRtn_code(E_BIS_MDL_DEF + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("产品代码[" + ret_wo.getMdl_id_fk() + "]信息不存在");
			return;
		}

		//获取工艺路线信息
		Bis_pathiId bis_pathiId = new Bis_pathiId();
		bis_pathiId.setPath_id(ret_wo.getPath_id_fk());
		bis_pathiId.setPath_ver(ret_wo.getPath_ver_fk());
		Bis_pathi bis_pathi = bisPathiDao.get(bis_pathiId);
		if(bis_pathi==null){
			outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("工艺路线[" + ret_wo.getPath_id_fk()
					+ "][" + ret_wo.getPath_ver_fk() + "]不存在");
			return;
		}
		//check pathi hold
		String path_stat = bis_pathi.getPath_stat();
		if("HOLD".equals(path_stat)){
			outTrx.setRtn_code(E_FPPUPKCLM_PATH_STATUS_INVALIED + "");
			outTrx.setRtn_mesg("该订单的工艺路线已经HOLD");
			return;
		}
		//检查工艺路线起始站点设定
		Bis_pathId bis_pathId = new Bis_pathId();
		bis_pathId.setPath_id_fk(bis_pathi.getId().getPath_id());
		bis_pathId.setPath_ver_fk(bis_pathi.getId().getPath_ver());
		bis_pathId.setCr_ope_no(bis_pathi.getFirst_ope_no());
		Bis_path bis_path = bisPathDao.get(bis_pathId);
		if(bis_path==null){
			outTrx.setRtn_code(E_FPPUPKCLM_PATH_NO_FRST_OPE + "");
			outTrx.setRtn_mesg("工艺路线[" + bis_pathi.getId().getPath_id()
					+ "][" + bis_pathi.getId().getPath_ver() + "]未设置起始站点");
			return;
		}

		//获取起始站点信息
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
		bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());
		Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
		if(bis_ope==null){
			outTrx.setRtn_code(E_BIS_OPE + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("站点[" + bis_path.getCr_ope_id_fk()
					+ "][" + bis_path.getCr_ope_ver_fk() + "]信息没有维护");
			return;
		}

		//获取下一制程站点信息
		Bis_path nx_bis_path = new Bis_path();
		Bis_ope nx_bis_ope = new Bis_ope();
		RouteObj nxRouteObj = bisPathDao.getNxPath(bis_path.getId().getPath_id_fk(),
				bis_path.getId().getPath_ver_fk(), bis_path.getId().getCr_ope_no());
		if(_NORMAL == nxRouteObj.getLRc()){
			nx_bis_path = nxRouteObj.getPath();
			nx_bis_ope = nxRouteObj.getOpe();
		}else if(PATH_IS_FINISH == nxRouteObj.getLRc()){
			;
		}else{
			outTrx.setRtn_code(E_BIS_OPE + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("获取下一制程站点错误：" + nxRouteObj.getGszMsgBuf());
			return;
		}
		//get tool_id
		String cr_tool_id_fk = "";
		if(null != nx_bis_ope){
			String sql = "SELECT * from BIS_TOOL_GR g LEFT JOIN BIS_OPE o on o.toolg_id_fk = g.toolg_id where 1=1 and o.proc_id = '"+nx_bis_ope.getProc_id()+"'";
			Session session = bisToolGrDao.getCrSession();
			List<Bis_tool_gr> bis_tool_grList = session.createSQLQuery(sql).addEntity(Bis_tool_gr.class).list();
			if(bis_tool_grList.size() >0){
				String nx_tool_id =bis_tool_grList.get(0).getId().getTool_id_fk();
				Bis_tool bis_tool= bisToolDao.get(nx_tool_id);
				if(null != bis_tool){
					cr_tool_id_fk = bis_tool.getTool_id();
				}
			}
		}
		List<FppupkclmOA> oary = new ArrayList<FppupkclmOA>();
        for(Ret_prd_indt ret_prd_indt:prds){
			Ret_prd_info ret_prd_info = new Ret_prd_info();
			//Set Prd_info
			ret_prd_info = new Ret_prd_info();
			ret_prd_info.setPrd_seq_id(ret_prd_indt.getVdr_prd_seq_id());
			ret_prd_info.setWo_id_fk(woId);
			ret_prd_info.setLot_id(lotId);
			ret_prd_info.setRsn_code(rsnCode);
			ret_prd_info.setMdl_id_fk(bis_mdl_def.getMdl_id());
			ret_prd_info.setSo_id_fk(ret_wo.getSo_id());
			ret_prd_info.setMtrl_prod_id_fk(mtrlIdFk);
			ret_prd_info.setPrd_seq_typ("P");
			ret_prd_info.setPrd_qty(1);

			if("OK".equals(prdGrade)){
				String hqlgrd = "From Bis_mdl_grd where 1=1 and cus_id_fk ='Y'and prd_judge ='OK'";
				List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hqlgrd);
				if(bis_mdl_grds.size() >0){
					prdGrade =bis_mdl_grds.get(0).getPrd_grade();
				}else {
					prdGrade = "";
				}
				ret_prd_info.setPrd_grade(prdGrade);
				ret_prd_info.setPrd_stat("INPR");
				ret_prd_info.setCr_toolg_id_fk(bis_ope.getToolg_id_fk());
				ret_prd_info.setCr_ope_id_fk(bis_path.getCr_ope_id_fk());
				ret_prd_info.setCr_ope_no_fk(bis_path.getId().getCr_ope_no());
				ret_prd_info.setCr_ope_ver_fk(bis_path.getCr_ope_ver_fk());
				ret_prd_info.setCr_path_id_fk(bis_path.getId().getPath_id_fk());
				ret_prd_info.setCr_path_ver_fk(bis_path.getId().getPath_ver_fk());
				ret_prd_info.setCr_proc_id_fk(bis_ope.getProc_id());

				if(PATH_IS_FINISH == nxRouteObj.getLRc()){
					ret_prd_info.setNx_toolg_id_fk(_SPACE);
					ret_prd_info.setNx_ope_id_fk(_SPACE);
					ret_prd_info.setNx_ope_no_fk(_SPACE);
					ret_prd_info.setNx_ope_ver_fk(_SPACE);
					ret_prd_info.setNx_path_id_fk(_SPACE);
					ret_prd_info.setNx_path_ver_fk(_SPACE);
					ret_prd_info.setNx_proc_id_fk(_SPACE);
					ret_prd_info.setNx_tool_run_mode(_SPACE);
				}else{
					ret_prd_info.setNx_toolg_id_fk(nx_bis_ope.getToolg_id_fk());
					ret_prd_info.setNx_ope_id_fk(nx_bis_path.getCr_ope_id_fk());
					ret_prd_info.setNx_ope_no_fk(nx_bis_path.getId().getCr_ope_no());
					ret_prd_info.setNx_ope_ver_fk(nx_bis_path.getCr_ope_ver_fk());
					ret_prd_info.setNx_path_id_fk(nx_bis_path.getId().getPath_id_fk());
					ret_prd_info.setNx_path_ver_fk(nx_bis_path.getId().getPath_ver_fk());
					ret_prd_info.setNx_proc_id_fk(nx_bis_ope.getProc_id());
					ret_prd_info.setNx_tool_run_mode(nx_bis_ope.getTool_run_mode());
				}
			}else{
				String hqlgrd1 = "From Bis_mdl_grd where 1=1 and cus_id_fk ='Y'and prd_judge ='NG'";
				List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hqlgrd1);
				if(bis_mdl_grds.size() >0){
					prdGrade =bis_mdl_grds.get(0).getPrd_grade();
				}else {
					prdGrade = "";
				}
				ret_prd_info.setPrd_grade(prdGrade);
				ret_prd_info.setPrd_stat("WAIT");
				ret_prd_info.setNx_toolg_id_fk(bis_ope.getToolg_id_fk());
				ret_prd_info.setNx_ope_id_fk(bis_path.getCr_ope_id_fk());
				ret_prd_info.setNx_ope_no_fk(bis_path.getId().getCr_ope_no());
				ret_prd_info.setNx_ope_ver_fk(bis_path.getCr_ope_ver_fk());
				ret_prd_info.setNx_path_id_fk(bis_path.getId().getPath_id_fk());
				ret_prd_info.setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
				ret_prd_info.setNx_proc_id_fk(bis_ope.getProc_id());
			}
			ret_prd_info.setCr_tool_id_fk(cr_tool_id_fk);
			ret_prd_info.setEvt_usr(evtUsr);
			ret_prd_info.setEvt_cate(EVT_PNL_UNPACK);
			ret_prd_info.setEvt_timestamp(current_timestamp);
			ret_prd_info.setBatch_no(woId);
			ret_prd_info.setMtrl_box_id_fk(mtrlBoxId);
			retPrdInfoDao.save(ret_prd_info);
			ret_prd_indt.setPrd_stat("RLSE");
			ret_prd_indt.setEvt_usr(evtUsr);
			ret_prd_indt.setEvt_timestamp(current_timestamp);
			retPrdIndtDao.update(ret_prd_indt);
			FppupkclmOA fppupkclmOA = new FppupkclmOA();
			fppupkclmOA.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
			fppupkclmOA.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
			fppupkclmOA.setWo_id_fk(ret_prd_info.getWo_id_fk());
			fppupkclmOA.setPrd_stat(ret_prd_info.getPrd_stat());
			fppupkclmOA.setPrd_grade(ret_prd_info.getPrd_grade());
			fppupkclmOA.setSo_id_fk(ret_prd_info.getSo_id_fk());
			fppupkclmOA.setEvt_usr(ret_prd_info.getEvt_usr());
			fppupkclmOA.setEvt_timestamp(ret_prd_info.getEvt_timestamp());
			oary.add(fppupkclmOA);
		}


		//更新工单信息
		ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty() + prds.size());
		ret_wo.setWo_stat(WO_STATUS_RLSE);
		ret_wo.setEvt_usr(evtUsr);
		ret_wo.setEvt_timestamp(current_timestamp);
		retWoDao.save(ret_wo);

		ret_prd_in.setPrd_act_qty(0);
		ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_tl_qty());
		ret_prd_in.setEvt_usr(evtUsr);
		ret_prd_in.setEvt_timestamp(current_timestamp);
		retPrdInDao.update(ret_prd_in);



		outTrx.setRl_prd_qty(ret_wo.getRl_prd_qty());
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
}
